<template>
  <view>
    <!-- 搜索组件 -->
    <my-search-bar></my-search-bar>
    <!-- 轮播图 -->
    <swiper indicatorDots autoplay circular>
      <swiper-item v-for="item in swipers" :key="item.goods_id">
        <image :src="item.image_src" mode="scaleToFill" />
      </swiper-item>
    </swiper>
    <!-- 导航图 -->
    <view class="nav-list">
      <image
        class="nav-img"
        v-for="item in catitems"
        :key="item.name"
        :src="item.image_src"
        mode="scaleToFill"
        @click="goTocategory(item.name)"
      />
    </view>
    <!-- 楼层数据 -->

    <view v-for="item in floordata" :key="item.floor_title.name">
      <!-- 标题部分 -->
      <image
        class="floor-title"
        :src="item.floor_title.image_src"
        mode="scaleToFill"
      />

      <!-- 楼层图片区域 -->
      <view class="floor-img-box">
        <!-- 内容部分 -->
        <!-- 左边 -->

        <navigator :url="item.product_list[0].navigator_url">
          <image
            :style="{ width: item.product_list[0].image_width + 'rpx' }"
            :src="item.product_list[0].image_src"
            mode="widthFix"
          />
        </navigator>
        <!-- 右边 -->
        <view class="right-img-box">
          <navigator
            :url="subitem.navigator_url"
            v-for="(subitem, index) in item.product_list"
            :key="subitem.name"
          >
            <image
              :src="subitem.image_src"
              :style="{ width: subitem.image_width + 'rpx' }"
              v-show="index > 0"
              mode="widthFix"
            />
          </navigator>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import mySearchBar from "../../components/my-search-bar/my-search-bar.vue";
export default {
  components: { mySearchBar },
  data() {
    return {
      swipers: [], // 轮播图
      catitems: [], // 导航菜单
      floordata: [],
    };
  },
  //
  onLoad() {
    this.getSwipersData();
    this.getCatitemsData();
    this.getFloorData();
  },
  methods: {
    // 楼层数据
    async getFloorData() {
      const res = await uni.$request({
        url: "home/floordata",
      });
      // console.log(res);
      if (res.meta.status === 200) {
        res.message.forEach((item) => {
          item.product_list.forEach((subitem) => {
            subitem.navigator_url =
              "/subpkg/goods-list/index?" + subitem.navigator_url.split("?")[1];
          });
        });
        this.floordata = res.message;
      }
    },
    // 轮播图请求
    async getSwipersData() {
      const res = await uni.$request({
        url: "home/swiperdata",
      });
      //   console.log(res);

      if (res.meta.status === 200) {
        this.swipers = res.message;
      }
    },
    // 导航图请求
    async getCatitemsData() {
      const res = await uni.$request({
        url: "home/catitems",
      });
      //   console.log(res);
      if (res.meta.status === 200) {
        this.catitems = res.message;
      }
    },
    goTocategory(name) {
      if (name === "分类") {
        uni.switchTab({ url: "/pages/category/index" });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
swiper {
  height: 400rpx;

  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}
.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>
